Last Updated on March 3, 2024
If you work in IT, chances are you’ve had to edit a configuration file or two in your time. This is especially true if you work in an enterprise-level business, where not everything can be configured with the click of a button.
That’s right, the bigger your company gets, the more likely it is you’re going to have to do a bit of manual configuration. And when you get into containers, there is no escaping this reality. Configuration files are a necessity.
Thing is, there are many types of configuration file types. There’s the flat text files, bash scripts, JSON files, INI, TOML, and more. But no configuration file type has become as important as the YAML file.
Without YAML files, deploying containers at scale becomes significantly more challenging.
But what is a YAML file? Let’s find out.
YAML – By Definition
YAML is a recursive acronym that stands for “YAML Ain’t Markup Language”. YAML is a human-readable data-serialization language used for configuration files as well as applications where data is stored and/or transmitted.
YAML uses Python-style indentation to indicate nesting. It is that indentation that is crucial to creating a YAML file. Why?
Because if indentation isn’t done with absolute consistency, the configuration file will simply not be usable in the application it was written for. Inconsistent indenting is the stuff of QA testing services nightmares.
One thing that separates YAML from other languages is its flexibility, which is brought about because it is made up of bits and pieces of other languages. Some similarities to other languages include:
- From Perl – Scalars, lists, and associative arrays.
- From MIME – The document separator “—”.
- From C – Escape sequences.
- From HTML – Whitespace wrapping.
Two of the more important YAML features are:
- Delimiter collision resistance – YAML depends upon indentation for structure, which makes it resistant to delimiter collision, a problem that occurs when a character that is intended as part of the data gets interpreted as a delimiter instead.
- Security – YAML has no executable commands, making it more secure than some other languages.
YAML Basics
Let’s build a sample YAML file to better understand its structure. First, we start with YAML variables as well as scalars, which are defined as such:
NAME: VALUE
Where name is the name of the variable/scalar and VALUE is the value associated with the variable or scalar. These values can be integers, strings, floats, or boolean, like this:
variable: 10
string: “Ten”
float: 10.0
boolean: Yes
Associative arrays and lists are defined using either standard block or inline format similar to that found in JSON. So you could have a list in block form like this:
— # Music Genres
– rock
– pop
– funk
Or as an inline block like this:
— # Music Genres
[rock, pop, funk]
Next, we have strings. There are two ways to format strings in YAML, using the | or the > character.
Using the | character preserves newlines, like so:
string: |
The lines in
This string
Will be broken
Into four lines
If, on the other hand, we use the > character, the lines are formed into a single paragraph, like so:
string: >
The lines in
this string
will form a
single paragraph
YAML files also begin with three dashes at the top.
So now we have the beginning of the YAML files, variables, arrays, and strings. Let’s piece together a YAML file with this information. I’ll demonstrate by way of writing a YAML file that will hold data for the album “Empath” by Devin Townsend.
We’ll first start by defining our variables:
—
genre: “progressive rock”
era: 2019
artist: “Devin Townsend”
title: “Empath”
Now we’ll add a block array for the songs:
songs:
– “Castaway”
– “Genesis”
– “Spirits Will Collide”
– “Evermore”
– “Sprite”
– “Hear Me”
– “Why?”
– “Borderlands”
– “Requiem”
– “Singularity”
At this point, our YAML file looks like:
—
genre: “progressive rock”
era: 2019
artist: “Devin Townsend”
title: “Empath”
songs:
– “Castaway”
– “Genesis”
– “Spirits Will Collide”
– “Evermore”
– “Sprite”
– “Hear Me”
– “Why?”
– “Borderlands”
– “Requiem”
a- “Singularity”
Now, here’s something interesting. The Singularity song has six movements. Fortunately, YAML allows us to do multi-dimensional arrays, so we can add that like so:
– “Singularity”
–
– “I. Adrift”
– “II. I Am I”
– “III. There Be Monsters”
– “IV. Curious Gods”
– “V. Silicon Scientists”
– “VI. Here Comes the Sun!”
So now our file looks like:
—
genre: “progressive rock”
era: 2019
artist: “Devin Townsend”
title: “Empath”
songs:
– “Castaway”
– “Genesis”
– “Spirits Will Collide”
– “Evermore”
– “Sprite”
– “Hear Me”
– “Why?”
– “Borderlands”
– “Requiem”
– “Singularity”
–
– “I. Adrift”
– “II. I Am I”
– “III. There Be Monsters”
– “IV. Curious Gods”
– “V. Silicon Scientists”
– “VI. Here Comes the Sun!”
That YAML file would be saved with the .yaml extension (as in file.yaml). This, of course, is just an example to show you how the YAML file is laid out. The file above has little practical usage as a configuration file, but it serves well for the purpose of demonstration.
Why YAML is So Important
There is one crucial reason why YAML is so important to businesses. That reason is automation. By employing YAML config files, systems like Kubernetes can make use of other applications to make automation possible.
Because of the flexibility inherent with YAML, it is possible to craft highly complex container deployment files.
For any enterprise-level business, even those that might outsource the QA of software development, automation and complexity are key to being agile. Along with the automation of software like Kubernetes comes levels of failover, load balancing, and uptime you won’t see with non-automated systems.
And at the heart of those automated systems? YAML.
For IT administrators, the simplicity and elegance of YAML make the introduction of automation into a development pipeline much easier.